package VolumeMax;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;
import java.util.TreeMap;

public class VolumeMaxReducer extends Reducer<Text, VolumeMaxFlowBean, VolumeMaxFlowBean, Text> {
    private TreeMap<VolumeMaxFlowBean, Text> flowMap = new TreeMap<>();

    //    @Override
    protected void reduce(Text key, Iterable<VolumeMaxFlowBean> values, Reducer.Context context) throws IOException, InterruptedException {
        for (VolumeMaxFlowBean value :
                values) {
            VolumeMaxFlowBean flowBean = new VolumeMaxFlowBean(value.getSymbol(), value.getVolume());

//            Text phone = new Text(key);
            Text datetext = new Text(key);

//             1 向treeMap集合中添加数据
            flowMap.put(flowBean, datetext);
//            flowMap.put(value, key);

        }
    }

    @Override
    protected void cleanup(Reducer.Context context) throws IOException, InterruptedException {
        // 3 遍历集合，输出数据
        Iterator<VolumeMaxFlowBean> iterator = flowMap.keySet().iterator();

        while (iterator.hasNext()) {
            VolumeMaxFlowBean valueOut = iterator.next();
            Text keyOut = flowMap.get(valueOut);

            context.write(keyOut, valueOut);
        }

    }
}
